import {
  default as imageLayerUtils
} from "./imageLayer";
import {
  tilesetUtils
} from "./tileset";

const utils = {}

utils.addlayer = function(viewer, lyrDefine){
  if (viewer && lyrDefine) {
    if(tilesetUtils.add(viewer, lyrDefine)) return;
    if(imageLayerUtils.addImageLayer(viewer, lyrDefine)) return;
  }
}

utils.addlayers = function(viewer, lyrDefines){
  if (viewer && lyrDefines) {
    lyrDefines.forEach(e => {
      utils.addlayer(viewer, e)
    })
  }
}

utils.get = function(viewer, name){
  if(!viewer) return undefined;
  if(!name) return undefined;

  const { imageryLayers, entities, scene } = viewer;
  for(let i=0, n = imageryLayers.length; i<n; ++i){
    const lyr = imageryLayers.get(i)
    if(lyr.name===name){
      return lyr;
    }
  }

  const primitives = scene.primitives
  for(let i=0, n = primitives.length; i<n; ++i){
    const primitive = primitives.get(i)
    if(primitive.name===name){
      return primitive;
    }
  }
}

const layersUtils = utils;

export {
  layersUtils,
  tilesetUtils,
  imageLayerUtils,
}
